﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Extractor.Manager;
using Extractor.Manager.Entities;
using Infrastructure;
using SceneUnpacker.Schematrix;

namespace RarExtractor
{
    public partial class Main : UserControl
    {
        private List<ArchiveFile> _archiveFile;
        private int _fileCounter = 0;
        public Main ()
        {
            InitializeComponent();
            _archiveFile = new List<ArchiveFile>();

            ExtractorManager.Intance.Add( @"D:\My Projects\TestFolder" );
            ExtractorManager.Intance.RarFileFouned += Intance_RarFileFouned;
            ExtractorManager.Intance.DoneExtracting += Intance_DoneExtracting;
            ExtractorManager.Intance.ProgressChanged += Intance_ProgressChanged;
            //ExtractorManager.Intance.Start();
        }

        void ParentForm_Closing ( object sender, CancelEventArgs e )
        {
            SettingsManager<FormSettings>.Instane.Settings.ListViewState = _fastObjectListView.SaveState();
            SettingsManager<FormSettings>.Instane.SaveConfigurations();
        }

        void Intance_ProgressChanged ( object sender, ExtractionProgressEventArgs extractionProgressEventArgs )
        {

        }

        private void Intance_DoneExtracting ( object sender, DoneExtractingEventArgs e )
        {

        }

        private void Intance_RarFileFouned ( object sender, FoundedFileEventArgs e )
        {
            SettingsManager<ExtractorSettings>.Instane.Settings.ArchiveFiles.Add( e.File );
            SettingsManager<ExtractorSettings>.Instane.SaveConfigurations();
            _archiveFile.Add( e.File );
            InvokeIfRequired( () =>
            {
                _fastObjectListView.SetObjects( _archiveFile );
                _toolStripStatusLabelTotalFiles.Text = _archiveFile.Count.ToString();
            } );
        }

        private void fileToolStripMenuItem_Click ( object sender, EventArgs e )
        {

        }

        private void Main_Load ( object sender, EventArgs e )
        {
            ParentForm.Closing += new CancelEventHandler( ParentForm_Closing );

            if (SettingsManager<FormSettings>.Instane.Settings.ListViewState != null)
            {
                _fastObjectListView.RestoreState( SettingsManager<FormSettings>.Instane.Settings.ListViewState );
            }
            _archiveFile.AddRange( SettingsManager<ExtractorSettings>.Instane.Settings.ArchiveFiles );
            _fastObjectListView.SetObjects( _archiveFile );
            _toolStripStatusLabelTotalFiles.Text = _archiveFile.Count.ToString();
        }

        private void InvokeIfRequired ( Action action )
        {
            if (InvokeRequired)
                Invoke( action );
            else
                action();
        }

        private void optionsToolStripMenuItem_Click ( object sender, EventArgs e )
        {
            PreferencesDlg preferencesDlg = new PreferencesDlg();
            preferencesDlg.ShowDialog();
        }

    }
}
